﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Reflection;
using System.Collections.ObjectModel;
using System.AddIn.Hosting;
using SimpleAddinTest;
using System.Activities.Presentation.Toolbox;

namespace MultipleAppDomainTest
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        //WPFAddIn_ContractToViewHostSideAdapter adapter;
        DesignerHostAdapters hostView;
        public MainWindow()
        {
            InitializeComponent();
            this.InitialUI();
        }

        private void InitialUI()
        {
            //this.loadToolbox();
            this.LoadDesignAppDomain1();
            this.LoadDesignAppDomain2();
        }

        private void loadToolbox() 
        {
            //var toolboxWrappers = new List<ToolboxCategory>();
            //var toolbox = new ToolboxControl();

            //Utility.CreateBasicActivitiesToolbox(toolboxWrappers);
            //Utility.CreateAdvancedActivitiesToolbox(toolboxWrappers);
            //Utility.CreateUserActivitiesToolbox(Caching.ActivityAssemblyItems, toolboxWrappers);
            
            //foreach (var category in toolboxWrappers)
            //{
            //   // this.tool.Categories.Add(category);
            //}
        }

        public void LoadDesignAppDomain1()
        {
            AppDomainSetup adSetup = new AppDomainSetup
            {
                LoaderOptimization = LoaderOptimization.MultiDomainHost,
            };
            AppDomain childDomain = AppDomain.CreateDomain("DesignerDomain1", null, adSetup);
            this.hostView = new DesignerHostAdapters((IDesignerContract)childDomain.CreateInstanceAndUnwrap(typeof(DesignerAddIn).Assembly.FullName, typeof(DesignerAddIn).FullName));
            FrameworkElement designSurface = this.hostView.GetDesignView();
            this.wf1.Content = designSurface;
        }

        public void LoadDesignAppDomain2()
        {
            AppDomainSetup adSetup = new AppDomainSetup
            {
                LoaderOptimization = LoaderOptimization.MultiDomainHost,
            };
            AppDomain childDomain = AppDomain.CreateDomain("DesignerDomain2", null, adSetup);
            this.hostView = new DesignerHostAdapters((IDesignerContract)childDomain.CreateInstanceAndUnwrap(typeof(DesignerAddIn).Assembly.FullName, typeof(DesignerAddIn).FullName));
            FrameworkElement designSurface = this.hostView.GetDesignView();
            this.wf2.Content = designSurface;
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            string fileName1 = ".\\1.0.0.0\\TestAT1.dll";
            string fileName2 = ".\\2.0.0.0\\TestAT1.dll";
            ImportAssemblyViewModel im1 = new ImportAssemblyViewModel(fileName1);
            im1.SelectedCategory = "Admin";
            im1.ImportCommand.Execute();

            ImportAssemblyViewModel im2 = new ImportAssemblyViewModel(fileName2);
            im2.SelectedCategory = "Admin";
            im2.ImportCommand.Execute();
        }
    }
}
